<?php
namespace App\Admin\Controllers\Traits\WxSettingTraits;

use App\Admin\Renderable\UserAvatarFrameRender;
use App\Models\WxClause;
use App\Models\User\WxUserAvatarFrame;
use App\Wen\Utils\AdminUtils;
use App\Wen\Utils\FieldUtils;
use App\Wen\Utils\GatewayUtils;
use App\Wen\Utils\Settings;
use App\Wen\Utils\SiteUtils;
use Dcat\Admin\Form\NestedForm;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Widgets\Form;
use Dcat\Admin\Widgets\Tooltip;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

trait formPageVipTrait{
    public function index_pagevip(Content $content)
    {
        return $content->title('会员系统')
//            ->body($this->buildPreviewButton())
//            ->body($this->newline())
            ->body(
                <<<HTML
<div class="card">{$this->form_pagevip()->render()}</div>
HTML
            );
    }
    protected function form_pagevip(){
        $form = new Form();
        $form->confirm('您确定要提交表单吗？', 'content');
        $form->disableResetButton();
        $form->tab('会员页面', function (Form $form) {
            $form->html(function () {
                return '';
            }, '<b>一、会员套餐</b>');
            $form->html(function () {
                $res16 = __system__paycode__tip__(16);
                Tooltip::make('.mini_global_admin_open_not_support_tip')
                    ->bottom()
                    ->title($res16['tip']);
                return $res16['text']."&nbsp;&nbsp;&nbsp;&nbsp;下方选项示例";
            }, '');
            $form->html(function () {
                return '<img style="width: 130px;" src="https://img.mini.minisns.cn/images/vip_options2.jpg">';
            }, '');
            $form->table('app_vip_options','套餐配置', function (NestedForm $table) {
                $table->number('days', '会员时长/天')->default(30)->required();
                $table->text('sales', '促销信息');
                $table->number('price', '价格')->default(10)->required();
                $table->number('price2', '划线价')->help('只做展示作用')->default(15);
                $table->number('coins', '赠送金币')->default(0);
                $table->switch('select', '默认选中')->help('仅能开启一个')->default(false);
            })->default(Settings::get('app_vip_options', ''));
            $form->html(function () {
                return '';
            }, '<b>二、会员邀请</b>');
            $form->number('app_vip_5_invitees_days', '5人兑换多少天会员')->help('5人可以兑换一次，已经兑换过了的邀请不能二次兑换，可以利用邀请无限兑换会员')->default(Settings::get('app_vip_5_invitees_days', 90));
            $form->html(function () {
                return '';
            }, '<b>三、会员权益</b>');
            $form->text('app_vip_benefits_title', '标题')->default(Settings::get('app_vip_benefits_title', '会员尊享5大特权'));
            $form->table('app_vip_benefits','会员权益', function (NestedForm $table) {
                $table->text('text', '文本')->default('尊贵身份');
                $table->image('img', '图片')->help('<a href="https://img.mini.minisns.cn/images/vip/haoqi.png" target="_blank" style="color: steelblue">点击下载默认图</a>')->url('files/uploads')->uniqueName()->autoUpload()->default('https://img.mini.minisns.cn/images/vip/haoqi.png');
                $table->radio('type', '类型')->options(FieldUtils::getUrlTypes())->default(0);
                $table->text('target', '跳转id')->help('<a href="https://doc.minisns.cn/doc/44/" target="_blank">路径大全</a>');

            })->default(Settings::get('app_vip_benefits', '[{"text":"\u5c0a\u8d35\u8eab\u4efd","img":"https:\/\/img.mini.minisns.cn\/images\/vip\/haoqi.png","type":"0","target":null},{"text":"\u5934\u50cf\u6302\u4ef6","img":"https:\/\/img.mini.minisns.cn\/2023\/04\/27\/88c24e9a541a62142ec10b0df10766b0.png","type":"0","target":null},{"text":"\u533f\u540d\u8bf4\u8bf4","img":"https:\/\/img.mini.minisns.cn\/2023\/04\/27\/06a8778489badf6ea8d10b2f820747e8.png","type":"0","target":null},{"text":"\u70ed\u95e8\u63a8\u8350","img":"https:\/\/img.mini.minisns.cn\/2023\/04\/27\/d4e95d7b26fbf09407fda75a1a3de275.png","type":"0","target":null},{"text":"\u66f4\u591a\u7279\u6743","img":"https:\/\/img.mini.minisns.cn\/2023\/04\/27\/ff3c1bb2e671a8a048fe499c48692d29.png","type":"0","target":null}]'));

            $form->html(function () {
                return '';
            }, '<b>四、会员头像框</b>');
            $form->multipleSelectTable('app_vip_user_avatar_frames', '头像框')
                ->title('会员头像框')
                ->max(100)
                ->from(UserAvatarFrameRender::make())
                ->model(WxUserAvatarFrame::class, 'id', 'name', 'selectTable:formPageVipTrait:app_vip_user_avatar_frames')
                ->help('你应该选择会员专属的头像框，点击<a href="'.admin_url('avatar_frame/frame').'" target="_blank">管理头像框仓库</a>，  <a href="https://doc.minisns.cn/doc/70/" target="_blank" style="color: red;">（ps：如何控制顺序?）</a>')
                ->default(Settings::get('app_vip_user_avatar_frames', ''));
            $form->html(function () {
                return '';
            }, '<b>五、会员弹窗及协议</b>');
            $form->image('members_popup_poster', '会员弹窗海报')->help('<a href="https://img.mini.minisns.cn/icons/vip.png" download="members_popup_poster.jpg" target="_blank" style="color: steelblue">点击下载默认图</a>，他人在你的个人主页点击会员标志时弹出，自己点击自己的会员标志是进入会员界面')->url('files/uploads')->uniqueName()->autoUpload()->default(Settings::get('members_popup_poster', 'https://img.mini.minisns.cn/icons/vip.png'));
            $form->select('app_vip_clause', '《会员协议》')->options(WxClause::pluck('title', 'id'))->help('<a href="'.admin_url('clause').'" target="_blank">进入条款管理</a>')->default(Settings::get('app_vip_clause', 0));
        });
        $form->tab('会员特权', function (Form $form) {
            $form->html(function () {
                return '';
            }, '<b>一、内容付费购买</b>');
            $form->rate('app_vip_paycontent_discount', '会员减少')->help('余额付费内容和金币付费内容都可以获得折扣，设置成100则代表会员可以免费查看所有付费内容')->value(Settings::get('app_vip_paycontent_discount', 0))->width(4);
            $form->html(function () {
                return '';
            }, '<b>二、会员免广告</b>');
            $form->radio('app_vip_ad_shut', '开关')->options([0=>'关闭', 1=>'开启'])->default(Settings::get('app_vip_ad_shut', 0));
            $form->html(function () {
                return '';
            }, '<b>三、会员匿名笔记</b>');
            $form->radio('app_vip_anonymous_only', '开关')->options([0=>'关闭', 1=>'开启'])->default(Settings::get('app_vip_anonymous_only', 0));
        });
        $form->action('settings/action/pagevip');
        return $form;
    }

    public function pagevip(Request $request){
        SiteUtils::forget_config_data();
        Settings::set('app_vip_options', AdminUtils::_table_data($request->input('app_vip_options')), true);

        Settings::set('app_vip_benefits_title', $request->input('app_vip_benefits_title'), true);
        Settings::set('app_vip_benefits', AdminUtils::_table_data($request->input('app_vip_benefits')), true);
        Settings::set('members_popup_poster', $request->input('members_popup_poster'), true);
        Settings::set('app_vip_clause', $request->input('app_vip_clause'), true);

        $avatar_frames = $request->input('app_vip_user_avatar_frames');
        $avatar_frames_ = [];
        if($avatar_frames){
            foreach (explode(',', $avatar_frames) as $avatar_frame_id){
                $avatar_frames_[] = $avatar_frame_id;
            }
        }


        Settings::set('app_vip_5_invitees_days', $request->input('app_vip_5_invitees_days'), true);
        Settings::set('app_vip_user_avatar_frames', json_encode($avatar_frames_), true);

        Settings::set('app_vip_paycontent_discount', _between_((int)($request->input('app_vip_paycontent_discount')), 0, 100), true);
        Settings::set('app_vip_ad_shut', $request->input('app_vip_ad_shut') ? 1 : 0, true);
        Settings::set('app_vip_anonymous_only', $request->input('app_vip_anonymous_only') ? 1 : 0, true);


        $__global_config_version__ = Settings::get('__global_config_version__', 1) + 1;
        Settings::set('__global_config_version__', $__global_config_version__);
        GatewayUtils::success('all', 13, [ 'version' => $__global_config_version__, 'file' => basename(__FILE__) ]);
        return $this->response()->success('成功！');
    }

}
